﻿using AutoMapper;
using BasicInformation.Domain;
using MediatR;
using SCM.Domain;
using System.Security.Claims;
using Warehouse.Api.Write.AllCation.Command.Stoking;
using Warehouse.Domian.Model;
using Warehouse.ErrorCode;
using Warehouse.Infrastructrue.Implenment;

namespace Warehouse.Api.Write.AllCation.Hanlder
{
    public class AddStockingHanlder : IRequestHandler<AddStockingCommand, ApiResult<int>>
    {
        public readonly IBaseRepostity<Stocking> _stockingRepostity;
        public readonly IMapper mapper;
        public readonly ILogger<AddStockingHanlder> logger;
        private readonly IHttpContextAccessor _contextAccessor;
        DbContext db;

        public AddStockingHanlder(IBaseRepostity<Stocking> stockingRepostity, IMapper mapper, IHttpContextAccessor contextAccessor, ILogger<AddStockingHanlder> logger, DbContext db)
        {
            _stockingRepostity = stockingRepostity;
            this.mapper = mapper;
            _contextAccessor = contextAccessor;
            this.logger = logger;
            this.db = db;
        }
        /// <summary>
        /// 添加出入库的信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public Task<ApiResult<int>> Handle(AddStockingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var model = mapper.Map<Stocking>(request);
                request.Bacth = DateTime.Now.ToString("yyyyMMdd");
                var userIdClaim = _contextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
                if (!int.TryParse(userIdClaim, out int userId))
                {
                    result.Code = ApiEume.Error;
                    result.Msg = "无法获取有效的用户Id";
                    return Task.FromResult(result);
                }
               
                var stuy = db.SqlSugar.Queryable<PurchaseManagementModel>().Where(x => (x.State) == model.StateRc).FirstAsync();
                if (model.StateRc == 1||stuy.Status==0)
                {
                    result.Code = ApiEume.Error;
                    result.Msg = $"找不到 采购的记录";
                    return Task.FromResult(result);
                }
                //判断表中的数据是入库还是出库
                int r = _stockingRepostity.InsertAsync(model).Result;
                if (r > 0)
                {
                    result.Msg = "添加成功";
                    result.Data = r;
                    result.Code = ApiEume.Success;
                    logger.LogInformation("添加出入库信息成功");
                }
                else
                {
                    result.Msg = "添加失败";
                    logger.LogInformation("添加出入库信息失败");
                    result.Data = r;
                    result.Code = ApiEume.Error;
                }
            }
            catch (Exception ex)
            {
                logger.LogInformation("添加出入库信息失败", ex);
            }
            return Task.FromResult(result);
        }
    }
}
